﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PeekService;

namespace PeekWebApp.Management.AjaxHandler
{
    /// <summary>
    /// UserRegistryEditHandler 的摘要说明
    /// </summary>
    public class UserRegistryEditHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string action = context.Request["action"];
            if (action.Equals("Edit"))//编辑
            {
                string name = context.Request["name"].Trim();
                string remark = context.Request["remark"].Trim();
                string email = context.Request["email"].Trim();
                string password = context.Request["password"].Trim();
                string code = context.Request["hidCode"];
                PeekService.UserRegistry model = new PeekService.UserRegistryService().GetModel(code);
                if (model == null)
                {
                    model = new PeekService.UserRegistry();
                    model.Code = Guid.NewGuid().ToString();
                    model.CreateID = "";
                    model.CreateTime = DateTime.Now;
                    model.State = 0;
                    model.IsEmailVerification = true;
                    model.Sort = new PeekService.ExtendService().GetSort("UserRegistry");                   
                    model.UserName =PeekService.DecryptService.Encrypt(name);
                    model.PassWord = PeekService.DecryptService.Encrypt(password);
                    model.Email = PeekService.DecryptService.Encrypt(email);
                    model.Remark = remark;
                    model.ConfirmPassWord = CreateRondomStr();
                    new PeekService.UserRegistryService().Add(model);
                }
                else
                {
                    model.UserName = PeekService.DecryptService.Encrypt(name);
                    model.PassWord = PeekService.DecryptService.Encrypt(password);
                    model.Email = PeekService.DecryptService.Encrypt(email);
                    model.Remark = remark;
                    new PeekService.UserRegistryService().Update(model);
                }
                context.Response.Redirect("../UserRegistry/List.aspx");
            }
            else //绑定
            {
                string code = context.Request["hidCode"];
                PeekService.UserRegistry type = new PeekService.UserRegistryService().GetModel(code);
                context.Response.Write(PeekService.CommonService.ConvertToJSON(type));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 创建随记验证码
        /// </summary>
        /// <returns></returns>
        private string CreateRondomStr()
        {

            string c = "";
            string str = "abcde^&@jkdjlfabcdadJKD_()@#$#ADFDAjkdptaiguolvyBNoujkajdk[]{}'<>$#!我爱旅游，健康的健康放假啊就浪费空间案件发卡机的链接发肯定就分开的减肥的空间按空间看得见啊拉家带口与金卡就飞快地将；垃圾；单放机阿基督教罚款进口敌舰反季节来得及看录放机打卡机发卡酱豆腐可激发科技交流肯定就罚款垃圾";
            for (var i = 1; i < 8; i++)
            {
                int num = (int)Math.Round(double.Parse((str.Length - 1).ToString()));
                c += str.Substring(num, 1);
            }
            return c + DecryptService.Encrypt(DateTime.Now.ToString("yy-M/ddHHmss"));

        }
    }
}